package com.fly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fly.bean.po.ElectricityConsumptionPo;
import com.fly.bean.request.ElectricityConsumptionRequest;
import com.fly.bean.result.AABillResult;
import com.fly.mapper.ElectricityConsumptionMapper;
import com.fly.service.ElectricityConsumptionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

@Service
public class ElectricityConsumptionServiceImpl extends ServiceImpl<ElectricityConsumptionMapper, ElectricityConsumptionPo> implements ElectricityConsumptionService {
    @Autowired
    private ElectricityConsumptionMapper electricityConsumptionMapper;

    @Override
    public void testInsert() {
        // 插入数据
//        ElectricityConsumptionPo electricityConsumptionPo = new ElectricityConsumptionPo();
//        electricityConsumptionPo.setYearAndMonth("2025-05");
//        electricityConsumptionPo.setCommon(2795);
//        electricityConsumptionPo.setSecondBedroom(3946);
//        electricityConsumptionPo.setMasterBedroom(4368);
//        electricityConsumptionPo.setLivingRoom(3724);
//        electricityConsumptionPo.setCurrentConsumptionCommon(12);
//        electricityConsumptionPo.setCurrentConsumptionChang(8);
//        electricityConsumptionPo.setCurrentConsumptionZhao(4);
//        electricityConsumptionPo.setCurrentConsumptionMeng(5);
//        electricityConsumptionPo.setElectricityBill(26.2);
//        electricityConsumptionPo.setUnitPrice(0.90345);
//        electricityConsumptionPo.setCreateTime(LocalDateTime.now());
//        electricityConsumptionMapper.insert(electricityConsumptionPo);
    }

    @Override
    public ElectricityConsumptionPo queryByMonth(String month) {
        ElectricityConsumptionPo po = electricityConsumptionMapper.queryByMonth(month);
        return po;
    }

    @Override
    public void insertNewData(ElectricityConsumptionRequest request) {
        //检查当前月份是否已有数据
        String yearAndMonth = request.getYearAndMonth();
        ElectricityConsumptionPo po = queryByMonth(yearAndMonth);
        if (po != null) {
            throw new RuntimeException("当前月份已有数据，无需插入！");
        }
        //生成当前月份数据
        String[] split = yearAndMonth.split("-");
        String year = split[0];
        String month = split[1];
        String contrastMonth;
        if (month.equals("01")) {
            //如果是1月份，需要跟去年的12月份对比
            contrastMonth = ((Integer.parseInt(year)) - 1) + "12";
        } else {
            int i = (Integer.parseInt(month)) - 1;
            contrastMonth = year + "-" + (i > 9 ? i : "0" + i);
        }
        //上个月的数据
        ElectricityConsumptionPo contrastPo = queryByMonth(contrastMonth);
        ElectricityConsumptionPo electricityConsumptionPo = new ElectricityConsumptionPo();
        //对比数据，生成本月数据
        if (contrastPo != null) {
            BeanUtils.copyProperties(request, electricityConsumptionPo);
            electricityConsumptionPo.setCurrentConsumptionCommon(electricityConsumptionPo.getCommon() - contrastPo.getCommon());
            electricityConsumptionPo.setCurrentConsumptionChang(electricityConsumptionPo.getSecondBedroom() - contrastPo.getSecondBedroom());
            electricityConsumptionPo.setCurrentConsumptionZhao(electricityConsumptionPo.getMasterBedroom() - contrastPo.getMasterBedroom());
            electricityConsumptionPo.setCurrentConsumptionMeng(electricityConsumptionPo.getLivingRoom() - contrastPo.getLivingRoom());
            //对比后的电量，加在一起是当月总用电量（度）
            int all = electricityConsumptionPo.getCurrentConsumptionCommon() + electricityConsumptionPo.getCurrentConsumptionChang() + electricityConsumptionPo.getCurrentConsumptionZhao() + electricityConsumptionPo.getCurrentConsumptionMeng();
            //电量单价四舍五入
            BigDecimal bigDecimal = new BigDecimal(electricityConsumptionPo.getElectricityBill() / all).setScale(1, RoundingMode.HALF_UP);
            electricityConsumptionPo.setUnitPrice(bigDecimal.doubleValue());
        } else {
            BeanUtils.copyProperties(request, electricityConsumptionPo);
        }
        electricityConsumptionPo.setCreateTime(LocalDateTime.now());
        electricityConsumptionMapper.insert(electricityConsumptionPo);
    }

    @Override
    public AABillResult AABill(String yearAndMonth) {
        ElectricityConsumptionPo po = queryByMonth(yearAndMonth);
        if(po == null){
            throw new RuntimeException("当前月份无可用数据...");
        }
        AABillResult aabillResult = new AABillResult();
        //取出各自用电量
        Integer currentConsumptionCommon = po.getCurrentConsumptionCommon();
        Integer currentConsumptionChang = po.getCurrentConsumptionChang();
        Integer currentConsumptionZhao = po.getCurrentConsumptionZhao();
        Integer currentConsumptionMeng = po.getCurrentConsumptionMeng();
        //计算各自所需支付的钱数
        Double unitPrice = po.getUnitPrice();
        double common = currentConsumptionCommon * unitPrice;
        double chang = currentConsumptionChang * unitPrice;
        double zhao = currentConsumptionZhao * unitPrice;
        double meng = currentConsumptionMeng * unitPrice;
        //公共用电均摊
        double AA = common / 3;
        aabillResult.setChang(chang + AA);
        aabillResult.setZhao(zhao + AA);
        aabillResult.setMeng(meng + AA);
        return aabillResult;
    }
}